package com.ray.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.MessageNotWriteableException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import com.ray.bean.MailConfigBean;
import com.ray.mapper.CompanyEmailMapper;
import com.ray.util.MailUtil;
import com.ray.util.ReturnStringUtil;
import com.ray.util.StringUtil;

import net.sf.json.JSONArray;


@Service
public class CompanyEmailService {

	@Autowired
	CompanyEmailMapper companyEmailMapper;
	
    
	/**
	 * @name 获取本公司所有邮箱
	 * @isUrl true
	 * @param loginCompany
	 * @param map
	 * @return
	 */
	public List<MailConfigBean> getCompanyEmailList(Map map) {
		return companyEmailMapper.getCompanyEmailList(map);
	}
	
	
	
	
	/**
	 * @name 添加公司邮箱
	 * @isUrl true
	 * @param host(服务器)
	 * @param port(端口)
	 * @param type(类型)
	 * @param status(状态)
	 * @param email(邮箱)
	 * @param password(密码)
	 * @param map
	 * @return
	 */
	public String addCompanyEmail(Map map) {
		Assert.isTrue(map.get("type") != null && !"".equals(map.get("type").toString()),"未获取到类型");
		Assert.isTrue(map.get("status")!= null && !"".equals(map.get("status").toString()),"未获取到状态");
		Assert.isTrue(map.get("host")!= null && !"".equals(map.get("host").toString()),"未获取到服务器地址");
		Assert.isTrue(map.get("port")!= null && !"".equals(map.get("port").toString()),"未获取到服务器端口");
		Assert.isTrue(map.get("email")!= null && !"".equals(map.get("email").toString()),"未获取到邮箱");
		Assert.isTrue(map.get("password")!= null && !"".equals(map.get("password").toString()),"未获取到密码");
		Assert.isTrue(this.checkTypeMailIsHave(map),"该类型的邮件已存在");
		map.put("id", StringUtil.getUUID());
		int row=companyEmailMapper.addCompanyEmail(map);
		return row>0?ReturnStringUtil.ADD_SUCCESS:ReturnStringUtil.ADD_ERROR;
	}
	/**
	 * @name 修改公司邮箱
	 * @isUrl true
	 * @param id(id)
	 * @param host(服务器)
	 * @param port(端口)
	 * @param type(类型)
	 * @param status(状态)
	 * @param email(邮箱)
	 * @param password(密码)
	 * @param map
	 * @return
	 */
	public String updateCompanyEmail(Map map) {
		Assert.isTrue(map.get("id") != null && !"".equals(map.get("id").toString()),"未获取到id");
		Assert.isTrue(map.get("type") != null && !"".equals(map.get("type").toString()),"未获取到类型");
		Assert.isTrue(map.get("status")!= null && !"".equals(map.get("status").toString()),"未获取到状态");
		
		int status=Integer.parseInt(map.get("status").toString());
		MailConfigBean configBean=companyEmailMapper.getMailConfigById(map.get("id").toString());
		Assert.notNull(configBean,"该记录不存在");
		//如果状态是由禁用改为使用
		if(status==1 && configBean.getStatus()==2) {
			//校验是否存在相同类型的记录
			Assert.isTrue(this.checkTypeMailIsHave(map),"该类型的邮件已存在");
		}
		int row=companyEmailMapper.updateCompanyEmail(map);
		return row>0?ReturnStringUtil.UPDATE_SUCCESS:ReturnStringUtil.UPDATE_ERROR;
	}


	/**
	 * @name 校验相同类型的邮箱是否存在
	 * @isUrl false
	 * @param type(类型)
	 * @param status(状态)
	 * @param map
	 * @return
	 */
	private boolean checkTypeMailIsHave(Map map) {
		Map map2=new HashMap();
		map2.put("type", map.get("type"));
		map2.put("status", 1);
		map2.put("loginCompany", map.get("loginCompany"));
		List<MailConfigBean> list=this.getCompanyEmailList(map2);
		if(list==null || list.size()<=0) {
			return true;
		}
		return false;
	}
	
	/**
	 * @name 通过公司配置邮箱发送邮件
	 * @isUrl false
	 * @param toAddrs 邮件接收人的邮箱集合
	 * @param ccAddrs 邮箱接受抄送人邮箱集合
	 * @param subject 标题
	 * @param htmlText 具体内容
	 * @param company 公司  
	 * @param type 类型
	 * @return
	 */
	public String companyMailSend(String[] toAddrs,String[] ccAddrs,String subject,String htmlText,String company,int type,String fileName,String remoteUrl) {
		Map map=new HashMap();
		map.put("loginCompany", company);
		map.put("type", type);
		map.put("status", 1);
		List<MailConfigBean> list=this.getCompanyEmailList(map);
		Assert.isTrue(list!=null && list.size()>0,"还未添加该类型邮箱");
		try {
			MailUtil.sendCompanyMailHtml(toAddrs, ccAddrs, subject, htmlText, list.get(0), fileName, remoteUrl);
		}catch(Exception e) {
			Assert.notNull(null,"消息发送失败，请检查配置");
		}
		return "发送成功";
	}
}
